package site.xuxingcan;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuxingcan on 16-7-28.
 */
public class HttpPostServerInboundHandler extends ChannelInboundHandlerAdapter {
    private static final String FALSE = "false", TRUE = "true", PHONENUMBER = "phoneNumber", KEY = "key";
    private HttpPostRequestDecoder decoder;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof HttpRequest) {
            HttpRequest request = (HttpRequest) msg;
            if (HttpMethod.POST == request.getMethod()) {
                decoder = new HttpPostRequestDecoder(request);
            } else {
                decoder = null;
                String res = "please use post\n";
                FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                        HttpResponseStatus.OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8")));
                response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE, "text/plain");
                response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
                response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
                ctx.write(response);
                ctx.flush();
            }
        } else if (msg instanceof HttpContent && decoder != null) {
            HttpContent content = (HttpContent) msg;
            decoder.offer(content);
            List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas();
            for (InterfaceHttpData parm : parmList) {
                Attribute data = (Attribute) parm;
                System.out.println(data.getName() + " = " + data.getValue());
            }
            String res = "POST\n";
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8")));
            response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            response.headers().set(io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
            ctx.write(response);
            ctx.flush();
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
}
